<?php

namespace App\Http\Controllers\PhpCookBook\Time;

use App\BCAM\CMTime\CMCalendar;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    //
    public function createFromFormat(){
        $text = 'Birthday: May 11, 1918.';
        //http://www.enkj.com/idcnews/Article/20170109/11074
        //* - 随机字节直到下一个分隔/数字
        //F - 月份名称（January - December）
        //j - 一个月中的第几天，不带前导零
        //Y - 年份（例如 2013）
        $when = \DateTime::createFromFormat("*: F j, Y.|",$text);
        dd($when);
    }

    //计算两个日期之差
    //注意:这两个日期都是在1970年之前
    public function dateTimeDiff(){
        $first = new \DateTime("1965-05-10 7:20:20",new \DateTimeZone('America/New_York'));
        $second = new \DateTime("1962-11-06 6:20:20",new \DateTimeZone('America/New_York'));
        $diff = $second->diff($first);
        printf("The two dates have %d weeks,%s days, %d hours,%d minute,%d seconds",floor($diff->format('%a')/7),$diff->format('%a')%7,$diff->format('%h'),$diff->format('%i'),$diff->format('%s'));
    }

    /**
     * 日历
     */
    public function getCalendar(){
        //因为用特殊的路由方式,所以Illuminate\Http\Reques获取不到???
        $request = new \Illuminate\Support\Facades\Request();
        $calendar = new CMCalendar($request::get('month',1),$request::get('year',2017));
        print $calendar->html();
    }

    public function getDateTimeAdd(){
        $date = new \DateTime('2000-12-31 12:00:00');
        $interval = new \DateInterval('P0D');//M是月 D是天

        $date->add($interval);
        echo $date->format('Y-m-d H:i:s') . "\n";

        echo $time = \DateTime::createFromFormat('Y-m-d H:i:s','2017-10-25 18:00:00')->add(new \DateInterval('P'.(time() > strtotime('2017-10-25 18:00:00')?1:0 ). 'D'))->format('Y-m-d H:i:s');
    }

}
